import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../../generated/l10n.dart';
import '../../store/counter.dart';

class Index extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    //调用状态模块Counter
    Counter _model = Provider.of<Counter>(context, listen: false);

    //print(S.of(context).Test);

    return Scaffold(
      appBar: AppBar(
        title: Text("Index"),
      ),
      body: Center(
        //child: Text("This is new page Index"),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            RaisedButton(
              child: Text("Navigate to login"),
              onPressed: () {
                print('login onPressed');
                Navigator.pushNamed(context, "/login");
                //跳转并销毁当前页面
                //Navigator.pushNamedAndRemoveUntil(context, "/login", (route) => route == null);
              },
            ),
            RaisedButton(
              child: Text("Navigate to box"),
              onPressed: () {
                print('box onPressed');
                Navigator.pushNamed(context, "/box");
              },
            ),
            RaisedButton(
              child: Text("Navigate to home"),
              onPressed: () {
                print('home onPressed');
                Navigator.pushNamed(context, "/home");
                //Navigator.of(context).pushNamed("/home", arguments: "argumentsHome");
              },
            ),
            RaisedButton(
              child: Text("Navigate to Test"),
              onPressed: () {
                print('test onPressed');
                Navigator.pushNamed(context, "/test");
              },
            ),

            Text('测试语言包调用：${S.of(context).Test}'),

            //显示当前值，不会同步响应
            Container(
                margin: const EdgeInsets.only(top: 20),
                width: MediaQuery.of(context).size.width,
                padding: const EdgeInsets.all(20),
                alignment: Alignment.center,
                color: Colors.lightBlueAccent,
                child: Text('当前是：${_model.count}')),

            //显示当前值，会同步响应,其他页面直接这样调用即可
            Consumer<Counter>(
              builder: (context, model, child) {
                return Container(
                  margin: const EdgeInsets.only(top: 20),
                  width: MediaQuery.of(context).size.width,
                  padding: const EdgeInsets.all(20),
                  alignment: Alignment.center,
                  color: Colors.lightGreen,
                  child: Text(
                    '${model.count}',
                  ),
                );
              },
            ),

            //直接使用provider模型对象
            FlatButton(
                color: Colors.tealAccent,
                //onPressed: _model.increment,
                onPressed: () {
                  _model.increment();
                },
                child: Icon(Icons.add)),
            /*
            //使用消费者传递model对象也行，上面就不需要定义_model
            Consumer<Counter>(
              builder: (context, model, child) {
                return FlatButton(
                    color: Colors.tealAccent,
                    onPressed: model.increment,
                    child: Icon(Icons.add));
              },
            ),
            */
          ],
        ),
      ),
    );
  }
}
